package com.dsbj.icardservice.controller;

import com.alibaba.fastjson.JSONObject;
import com.dsbj.common.msg.ResponseMessage;
import com.dsbj.icardservice.service.WxUserService;
import com.dsbj.icardservice.utils.Constants;
import com.dsbj.icardservice.utils.strprocess.ObjectParse;
import com.dsbj.icardservice.utils.strprocess.RandomStrUtil;
import com.dsbj.icardservice.utils.strprocess.StrUtil;
import com.dsbj.icardservice.webserviceclient.dsbjpmc.JWSService;
import com.dsbj.icardservice.webserviceclient.dsbjpmc.WxUserResponseMessage;
import com.dsbj.starter.redis.utils.RedisHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/user")
public class WxUserController {

    @Autowired
    private JWSService jwsService;

    @Autowired
    private WxUserService wxUserService;
	
    @Autowired
	private RedisHelper redisHelper;
    
    
    @RequestMapping("/getUserByOpenId")
    public WxUserResponseMessage getUserByOpenId(String openId){
        return wxUserService.getWxUserByOpenId(openId);
    }

    @RequestMapping("/wxBindUser")
    public ResponseMessage wxBindUser(Integer type,String openId, String username, String password,String employeeNo,String phoneNo,String vCode){
        return wxUserService.wxBindUser(type,openId,username,password,employeeNo, phoneNo, vCode);
    }
    
    /**
     * 获取手机验证码,并且该验证码的有效期为60s。
     * 传入参数 phoneNum。
     * @param
     * @return
     * @author cjw 2019-10-21
     */
    @RequestMapping(value = "/getVCode")
    public ResponseMessage getVCode(String phoneNo){
        //解析传来的参数
        if(StringUtils.isBlank(phoneNo)){
            return ResponseMessage.error("手机号不能为空");
        }
		
		/**
		 * 分析该手机号对应的验证码是否已存在。
		 */
		String key= Constants.WX_BIND_VCODE_PREFIX +phoneNo;
		String vcodeRedis=(String) redisHelper.get(key);
		if(StringUtils.isNotBlank(vcodeRedis)){
		    ///直接发送
            System.out.println(vcodeRedis);
			//return ResponseMessage.okWithMsg("验证码未过期!");
		}else{
            String vcode= RandomStrUtil.getNumStr(6);
            //发送
            System.out.println(vcode);
            redisHelper.set(Constants.WX_BIND_VCODE_PREFIX+phoneNo,vcode,Constants.EXPIRE_WX_BIND_CODE);
        }
		
		/**
		 * 产生6位数字验证码，并存入redis缓存中，其有效期为60s。
		 */
		return ResponseMessage.ok();
    }
    
    
    
    
}
